'use strict';

// signup controller
app.controller('DesignerFormController', ['$scope', '$http', '$state', function($scope, $http, $state) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $(".datetimepicker_").datetimepicker({
        format: 'yyyy-mm-dd',
        language:"cn",
        weekStart: 1,
        todayBtn:  "linked",
        autoclose: true,
        startView: 4,
        minView: 2,
        todayHighlight: true,
        endDate : new Date()
    });
    //实例化编辑器
    $('#brandSummernote').summernote({
        height: 400,
        minHeight: 300,
        maxHeight: 500,
        focus: true,
        lang:'zh-CN',
        // 重写图片上传
        callbacks: {
            onImageUpload: function(files) {
                sendFile(files[0],'#brandSummernote');
            }
        }
    });
    function sendFile(file, $editable){

        var filename = false;
        try{
            filename = file['name'];
        } catch(e){
            filename = false;
        }
        if(!filename){
            $(".note-alarm").remove();
        }

        //以上防止在图片在编辑器内拖拽引发第二次上传导致的提示错误
        var data = new FormData();
        data.append("file", file);
        data.append("key",filename); //唯一性参数
        $.ajax({
            data: data,
            type: "POST",
            url: "/file/uploadimage",
            cache: false,
            contentType: false,
            processData: false,
            success: function(result) {
                if(!result){
                    alert("上传失败！");
                    return;
                }else{
                    console.log("上传成功！");
                    var resultData=eval("("+result+")");
                    $($editable).summernote('insertImage',resultData.url,resultData.fileName);
                }
            },
            error:function(error){
                alert("上传失败！");
                console.log(error);
                return;
            }
        });
    }
    $scope.designer = {
        userType:"1",
        headImageUrl:""
    };
    $scope.authError = null;
    $scope.registerDesigner = function() {
        $scope.authError = null;
        $scope.designer.brandIntroduction=$("#brandSummernote").summernote('code');
        console.log($scope.designer.brandIntroduction);
        // Try to create
        $http.post('/user/registerdesigner', $.param($scope.designer))
            .then(function(response) {
                if ( response.data) {
                    $state.go('access.waitingaudit');
                }else{
                    $scope.authError = response;
                }
            }, function(x) {
                $scope.authError = 'Server Error';
            });
    };

    $scope.changeRole=function(){
        $state.go("access.chooserole");
    }

    $scope.uploadPhotoImage=function(target){
        if(!target|| !target.files[0]){
            return;
        }
        var file=target.files[0];
        var data = new FormData();
        data.append("file", file);
        data.append("key",file['name']); //唯一性参数
        $.ajax({
            data: data,
            type: "POST",
            url: "/file/uploadimage",
            cache: false,
            contentType: false,
            processData: false,
            success: function(result) {
                if(!result){
                    alert("上传失败！");
                    return;
                }else{
                    console.log("上传成功！");
                    var resultData=eval("("+result+")");
                    $(target).prev().attr("value",resultData.url);
                    $(target).next().attr("src",resultData.url);
                    $scope.designer.headImageUrl=resultData.url;
                }
            },
            error:function(){
                alert("上传失败！");
                return;
            }
        });
    }
}]);
